package com.yb.guigu.pick.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yb.guigu.pick.auth.entity.SysPermission;
import com.yb.guigu.pick.auth.entity.SysRolePermission;
import com.yb.guigu.pick.auth.entity.SysUser;
import com.yb.guigu.pick.auth.entity.SysUserRole;
import com.yb.guigu.pick.auth.mapper.SysPermissionMapper;
import com.yb.guigu.pick.auth.mapper.SysRolePermissionMapper;
import com.yb.guigu.pick.auth.mapper.SysUserRoleMapper;
import com.yb.guigu.pick.auth.service.ISysUserService;
import com.yb.guigu.pick.security.bean.GuiguPickGrantedAuthority;
import com.yb.guigu.pick.security.bean.GuiguPickUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * Copyright (C), 2022-2023, 姚兵
 * Author: 32210
 * Date: 2023/6/13 17:08
 * FileName: GuiguPickUserDetailsServiceImpl
 * Description:
 */

@Service
@Slf4j
public class GuiguPickUserDetailsServiceImpl extends AbstractGuiguPickUserDetailsService {

    @Autowired
    ISysUserService sysUserService;

    @Autowired
    SysUserRoleMapper sysUserRoleMapper;

    @Autowired
    SysPermissionMapper sysPermissionMapper;


    @Autowired
    SysRolePermissionMapper sysRolePermissionMapper;


    @Override
    protected SysUser loadByUsername(String username) {

        log.info("load user username:{}",username);

        if(!StringUtils.hasText(username)){
            throw new UsernameNotFoundException("用户名不能为空");
        }

        return sysUserService.getOne(new LambdaQueryWrapper<SysUser>().eq(SysUser::getUsername,username));
    }

    @Override
    protected SysUser loadByEmail(String email) {

        log.info("load user email:{}",email);

        if(!StringUtils.hasText(email)){
            throw new UsernameNotFoundException("邮箱不能为空");
        }

        return sysUserService.getOne(new LambdaQueryWrapper<SysUser>().eq(SysUser::getEmail,email));
    }

    @Override
    protected SysUser loadByPhone(String phone) {

        log.info("load user phone:{}",phone);

        if(!StringUtils.hasText(phone)){
            throw new UsernameNotFoundException("手机号不能为空");
        }

        return sysUserService.getOne(new LambdaQueryWrapper<SysUser>().eq(SysUser::getPhone,phone));
    }

    @Override
    protected GuiguPickUser loadUser(SysUser sysUser) {

        if(Objects.isNull(sysUser)){
            throw new UsernameNotFoundException("用户不存在");
        }

        Integer userId = sysUser.getUserId();
        //根据userId查询permission

        List<SysUserRole> sysUserRoles = sysUserRoleMapper.selectList(new LambdaQueryWrapper<SysUserRole>().eq(SysUserRole::getUserId, userId));

        GuiguPickUser guiguPickUser=new GuiguPickUser();

        BeanUtils.copyProperties(sysUser,guiguPickUser);

        if(CollectionUtils.isEmpty(sysUserRoles)){
            return guiguPickUser;
        }
        List<Integer> roleIds = sysUserRoles.stream().map(SysUserRole::getRoleId).collect(Collectors.toList());

        List<SysRolePermission> rolePermissions = sysRolePermissionMapper.selectList(new LambdaQueryWrapper<SysRolePermission>().in(SysRolePermission::getRoleId, roleIds));

        if(CollectionUtils.isEmpty(rolePermissions)){
            return guiguPickUser;
        }

        List<Integer> permissionids = rolePermissions.stream().map(SysRolePermission::getPermissionId).collect(Collectors.toList());

        List<SysPermission> sysPermissions = sysPermissionMapper.selectList(new LambdaQueryWrapper<SysPermission>().in(SysPermission::getPermissionId, permissionids));

        List<GuiguPickGrantedAuthority> list = sysPermissions.stream().map(item -> new GuiguPickGrantedAuthority(item.getPermissionCode())).collect(Collectors.toList());





        guiguPickUser.setAuthorities(list);

        return guiguPickUser;
    }
}
